<?php

  class etudiant_c extends CI_Controller
  {

    public function __construct()
    {
      parent:: __construct();

      $this->lang->load("etudiant","french");
      $this->lang->load("calendar","french");

      $this->load->model('etudiant_m');
      $this->load->model('emploi_m');
      $this->load->model('document_attache_m');
      $this->load->model('social_networks_m');
      $this->load->model('promotion_m');
      $this->load->model('stage_projet_seminaire_m');
      $this->load->model('etudes_superieures_m');

      $this->load->helper("url");
      $this->load->helper('language');
      $this->load->helper('form');
  

     // $this->load->liste_ancien();
    }

    function index()
    {
      //$this->load->ancien_annuaire();
    }

    public function etudiant_details($id,$mess)
    {

      if ($mess == 'Affichage')
      {
        $message='';
      }
      else
      {
        $message = $mess;
      }
      
      $etudiant = $this->etudiant_m->get_etudiant($id);


      
      if(is_null($etudiant->GMET_PHOTO) or empty($etudiant->GMET_PHOTO))
      {
        $this->etudiant_m->initPhoto($etudiant->GMET_CODE,"images/photo_profile/none.jpg");
        $etudiant->GMET_PHOTO=$this->etudiant_m->getPhotoBD($etudiant->GMET_CODE);
      }
      $emplois = $this->emploi_m->getAllEmplois($etudiant->GMET_CODE);
      $files = $this->document_attache_m->getAllFilesById($etudiant->GMET_CODE);
      $networks = $this->social_networks_m->getAllSocialNetworksById($etudiant->GMET_CODE);
      $sns = $this->social_networks_m->getSocialNetworks();
      $promos = $this->promotion_m->getPromotionById($etudiant->GMET_CODE);
      $stages = $this->stage_projet_seminaire_m->getAllStageProjetSeminaire($etudiant->GMET_CODE);
      $studies = $this->etudes_superieures_m->getAllEtudesSuperieures($etudiant->GMET_CODE) ;
      echo var_dump($studies);
      $data["message"]=$message;
      $data['id'] = $id;
      $data["emplois"] = $emplois;
      $data["networks"] = $networks;
      $data["sns"] = $sns;
      $data["promos"] = $promos;
      $data["files"] = $files;
      $data["stages"] = $stages;
      $data["studies"] = $studies;
      $data["etudiant"] = $etudiant;

      $this->load->view('etudiant/etudiant_details',$data);
    }

    public function desactivate_file()
    {
      $this->document_attache_m->desactivateFile();
      $data['files'] = $this->document_attache_m->getAllFilesById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/files_div',$data);
    }

    public function desactivate_all_files()
    {
      $this->document_attache_m->desactivateAllFilesById();
      $data['files'] = $this->document_attache_m->getAllFilesById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/files_div',$data);
    }

    public function activate_file()
    {
      $this->document_attache_m->activateFile();
      $data['files'] = $this->document_attache_m->getAllFilesById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/files_div',$data);
    }

    public function activate_all_files()
    {
      $this->document_attache_m->activateAllFilesById();
      $data['files'] = $this->document_attache_m->getAllFilesById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/files_div',$data);
    }

    public function delete_file()
    {
      $this->document_attache_m->deleteFile();
      $data["files"] = $this->document_attache_m->getAllFilesById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/files_div',$data);
    }


    public function add_sns()
    {
      $this->social_networks_m->addSocialNetwork();
      $data["networks"]= $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function desactivate_sns()
    {
      $this->social_networks_m->desactivateSocialNetwork();
      $data["networks"]= $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function desactivate_all_sns()
    {
      $this->social_networks_m->desactivateAllSocialNetworksById();
      $data["networks"]= $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function activate_sns()
    {
      $this->social_networks_m->activateSocialNetwork();
      $data["networks"]= $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function activate_all_sns()
    {
      $this->social_networks_m->activateAllSocialNetworksById();
      $data["networks"]= $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function delete_sns()
    {
      $this->social_networks_m->deleteSocialNetwork();
      $data["networks"] = $this->social_networks_m->getAllSocialNetworksById($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/sns_div',$data);
    }

    public function form_add_emploi()
    {
      $data["emplois"]= $this->emploi_m->getAllEmplois($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_emploi_div',$data);
    }

    public function add_emploi()
    {
      //$this->emploi_m->addEmploi();
      $data["emplois"]= $this->emploi_m->getAllEmplois($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/emploi_div',$data);
    }

    public function form_edit_emploi()
    {
      $data["job"]= $this->emploi_m->getEmploi();
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_edit_emploi_div',$data);
    }

    public function edit_emploi()
    {
      $this->emploi_m->editEmploi();
      $data["emplois"]= $this->emploi_m->getAllEmplois($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/emploi_div',$data);
    }

    public function delete_emploi()
    {
      $this->emploi_m->deleteEmploi();
      $data["emplois"]= $this->emploi_m->getAllEmplois($this->input->post('GMET_CODE'));
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/emploi_div',$data);
    }


     public function form_edit_observation()
    {
      $data["etudiant"]= $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_edit_observation_div',$data);
    }


    public function edit_observation()
    {
      $this->etudiant_m->editRemarques();
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/obs_div',$data);
    }

    public function form_edit_address()
    {
      $data["etudiant"]= $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_edit_address_div',$data);
    }

    public function edit_address()
    {
      $this->etudiant_m->editAddress();
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/address_div',$data);
    }

    public function form_edit_mail_tel()
    {
      $data["etudiant"]= $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_edit_mail_tel_div',$data);
    }
    
    public function edit_mail_tel()
    {
      $this->etudiant_m->editMail_tel();
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/mail_tel_div',$data);
    }


    public function form_add_studies()
    {
      $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
      $this->load->view('etudiant/ajax/form_add_studies',$data);
    }

    public function add_studies()
    {

      if($_FILES['GMES_DIPLOME']['error'] == 0)
        {
            $config['upload_path'] = 'files/diplomes/'; 
            $config['allowed_types'] = 'jpeg|jpg|pdf';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            
            $this->load->library('upload', $config); 

            if ( !$this->upload->do_upload('GMES_DIPLOME'))
            {
                $data['error'] = $this->upload->display_errors(); 
                $this->load->view('etudiant/error/error_upload',$data);
            }
            else
            {
                $this->etudes_superieures_m->addEtudesSuperieures();
                $data["studies"]= $this->etudes_superieures_m->getAllEtudesSuperieures($this->input->post('GMET_CODE'));
                $data["etudiant"] = $this->etudiant_m->get_etudiant($this->input->post('GMET_CODE'));
                $this->load->view('etudiant/ajax/studies_div',$data);
            }
        }
        else
        {
          redirect("etudiant_c/etudiant_details/".$this->input->post('GMET_CODE')."/4");
        }
    }






    public function modification_etudiant($id)
    {
      $message='Modification de la fiche personnelle';
      
      $etudiant = $this->etudiant_m->get_etudiant($id);

      $promo = $this->promotion_m->getAllPromotion();
      
      $data["message"]=$message;
      $data['id'] = $id;
      $data["etudiant"] = $etudiant;
      $data["promo"] = $promo;
     
      $this->load->view('etudiant/etudiant_modification',$data);
    }

    public function submit()
    {
      $result_submit = $_POST;
      $message = $this->etudiant_m->commit($result_submit);

      $data['message']=$message;
      $data['id'] = $result_submit['GMET_CODE'];

      $this->etudiant_details($data['id'],$data['message']);
    }

    public function edit_profilepic()
    {
        if($_FILES['GMET_PHOTO']['error'] == 0)
        {
            //upload and update the file
            $config['upload_path'] = 'images/photo_profile/'; // Location to save the image
            $config['allowed_types'] = 'gif|jpeg|jpg|png';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            //$config['max_size']   = '100';// in KB // if required, remove the comment and give the size
            //var_dump($config);
            //
            /*if(is_file('../public/images/MBDS.png'))
            {
              die('woohoo, folder and file found');
            }*/
            $this->load->library('upload', $config); //codeigniter default function
            /*var_dump(is_dir($config['upload_path']));
            var_dump(is_writeable($config['upload_path']));*/

            if ( !$this->upload->do_upload('GMET_PHOTO'))
            {
                redirect("etudiant_c/etudiant_details/".$this->input->post('id')."/1"); // redirect  page if the load fails.
            }
            else
            {
                //Image Resizing
                $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                $config['height'] = 300; // image re-size  properties
                $config['width'] = 1; // image re-size  propertiees
                $config['maintain_ratio'] = true;
                $config['master_dim'] = 'height';
 
                $this->load->library('image_lib', $config); //codeigniter default function
 
                if ( ! $this->image_lib->resize())
                {
                    redirect("etudiant_c/etudiant_details/".$this->input->post('id')."/2"); // redirect  page if the resize fails.
                }
 
                $this->etudiant_m->updatePhoto($this->input->post('id')); // here we are using the tank auth library for user log-in. we are getting the user id and updating the resized image as that particular's avatar.
                //echo $this->upload->display_errors();
                redirect("etudiant_c/etudiant_details/".$this->input->post('id')."/3");
            }
        }
        else
        {
        //show an error  to select a picture before clicking the update pic button
        redirect("etudiant_c/etudiant_details/".$this->input->post('id')."/4");
        }
    }

    public function add_file()
    {
        if($_FILES['GMDA_DOCUMENT']['error'] == 0)
        {
            $config['upload_path'] = 'files/document_attache/'; 
            $config['allowed_types'] = 'pdf';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            
            $this->load->library('upload', $config); 

            if ( !$this->upload->do_upload('GMDA_DOCUMENT'))
            {
                redirect("etudiant_c/etudiant_details/".$this->input->post('GMET_CODE')."/1"); 
            }
            else
            {
                $this->document_attache_m->insertFile();
                redirect("etudiant_c/etudiant_details/".$this->input->post('GMET_CODE')."/3");
            }
        }
        else
        {
          redirect("etudiant_c/etudiant_details/".$this->input->post('GMET_CODE')."/4");
        }
    }
  }
?>